
function adjustLayout(distance) {
    var MEDIUM_DISTANCE = 1.2;
    var FAR_DISTANCE = 2;
    distance = Math.round(distance * 100) / 100;
	
	var headerElement, contentElement, sideBar;
	headerElement = $(".ProfileCanopy");
	contentElement = $("div[data-test-selector='ProfileTimeline']");
	contentContainer = $(".u-size3of4");
	sideBar = $(".ProfileSidebar");

    if (distance < MEDIUM_DISTANCE) {
		if (contentElement.width() > 600 && contentElement.queue().length == 0) {
            contentElement.animate({ "width": "522px!important" }, function() {$(this).attr('style',"");});
            contentContainer.attr('style',"");
            sideBar.slideDown();
        }
        if (sideBar.width() <= 525 && !headerElement.is(':visible') && headerElement.queue().length == 0) {
            headerElement.slideDown();
        }
        //$(".profile-header-inner").slideDown();
        contentElement.find("*").css("font-size", "14px")
        contentElement.find("*").css("line-height", "18px")
    }
    if (distance >= MEDIUM_DISTANCE && distance < FAR_DISTANCE) {
        if (headerElement.queue().length == 0)
            headerElement.slideUp();
        if (!headerElement.is(':visible')) {
            if (contentElement.queue().length == 0) {
				contentElement.animate({ "width": "522px!important" }, function() {$(this).attr('style',"");});
				contentContainer.attr('style',"");
                sideBar.slideDown();
            }
        }
        //$(".profile-header-inner").slideDown();
        contentElement.find("*").css("font-size", distance * 14 + "px")
        contentElement.find("*").css("line-height", distance * 18 + "px")
    }

    if (distance >= FAR_DISTANCE) {
        if (headerElement.queue().length == 0)
            headerElement.slideUp();
        if (!headerElement.is(':visible')) {
            if (contentElement.queue().length == 0) {
                contentElement.animate({ "width": "100%!important" }, function() {$(this).attr('style',"width:100%!important");});
                contentContainer.animate({ "width": "100%!important" }, function() {$(this).attr('style',"width:100%!important");});
                sideBar.slideUp();
            }
        }
        //$(".profile-header-inner").slideUp();
        contentElement.find("*").css("font-size", distance * 14 + "px")
        contentElement.find("*").css("line-height", distance * 18 + "px")
    }
}

var gripStartPosition = {};
var gripStartScroll;
var gripActive = false;
function handleHandEvent(handEvent) {
    if (handEvent.HandEventType == 1) {
        gripActive = true;
        gripStartPosition["X"] = handEvent.X;
        gripStartPosition["Y"] = handEvent.Y;
        gripStartScroll = $(window).scrollTop();
        //$(".profile-field").html("GRIP");
    }
    if (handEvent.HandEventType == 2) {
        //$(".profile-field").html("GRIPRELEASE");
        gripActive = false;
    }
    if (gripActive) {
        /*$(".profile-field").html(
            gripStartScroll + 
            " + " + jKinect.scaleTo(handEvent, $(window).width(), $(window).height()).Y
            + " - " + jKinect.scaleTo(gripStartPosition,$(window).width(), $(window).height()).Y);*/
        $(window).scrollTop(gripStartScroll +
            + jKinect.scaleTo(handEvent, $(window).width(), $(window).height()).Y
            - jKinect.scaleTo(gripStartPosition, $(window).width(), $(window).height()).Y);
    }
}


$().ready(function () {
    jKinect.initKinect({}, null, Bind);
    $(document).keypress(function (event) {
        if (event.which >= 48 && event.which <= 49 + 10) {
            adjustLayout((event.which - 48)/2);
        }        
    });
});

function Bind() {

    jKinect.bind("PlayerStatus", function (status) {
        adjustLayout(status.Position.Z);
    });
    jKinect.bind("InteractionFrameReady", function (handEvent) {
        if (handEvent != null) {
            if (handEvent.HandPointers[0] != null && handEvent.HandPointers[0].IsActive)
                handleHandEvent(handEvent.HandPointers[0]);
            else if (handEvent.HandPointers[1] != null && handEvent.HandPointers[1].IsActive)
                handleHandEvent(handEvent.HandPointers[1]);
        }
    });
    setInterval("jKinect.getNextSkeletonFrame()", 200);
}